%{
#include <math.h>
#include "mycomplier.h"
#include "common.h"
int yylex(void);
void yyerror(char const *);

%}
%code requires {
  #include "program.h"
}
%define api.value.type union
%nterm <Exp_VarDefine *> typedec
%nterm < Symbol* >  tagdef
%nterm < list<Symbol>* > tagtail 
%token IF
%token < char*> COMPARE
%token < char*> ID "tag"
%token < char*> STRING "string"
%token < char*> TYPE "type"
%nterm < Condition* > statement
%token <int> NUM 
%token EUQL _E
%token _N
%%
input: typedec _E        {printf("get string is input\n");}
| IF statement '{' typedec _E '}'   {
                                    cout<<"if start"<<endl;
                                    cout<<"tmp "<<$2->temp()<<endl;
                                    }

;

statement: ID COMPARE NUM {
    cout<<"statement446664 "<<endl;
    $$=new Condition($1, to_string($3), $2);
};

typedec: TYPE tagdef    {
                cout<<"get string is typedec"<<endl;
                cout<<"tttt rr "<<$1<<endl;
                string type = $1;
                $$=new Exp_VarDefine(type); 
                };

tagdef: ID tagtail      {
                        printf("get string is tagdef \n");
                        cout<<"list addr "<<$2<<endl;
                        for(auto iter=$2->begin(); iter!= $2->end();iter++){
                            cout<<"iter "<<&iter<<endl;
                            cout<<"list "<<iter->gettmp()<<endl;
                        }
                        };

tagtail:                  {printf("get string is null \n");
                            list<Symbol> *l = new list<Symbol>;
                            $$=l;
                            }       
| ',' ID tagtail         {printf("get id is %s\n", $2);
                            Symbol *tmp=new Symbol();
                            tmp->backfillValue(&$2);
                            $3->push_back(*tmp);
                            $$=$3;
                        }
| '=' NUM                {printf("get num is %d\n", $2);
                            cout<<"ddddddd "<<$2<<endl; 
                            int tmp_value = $2;
                            Symbol *tmp=new Symbol();
                            //tmp->backfillValue(&tmp_value);
                            tmp->backfilltmp(123456);

                            list<Symbol> *l = new list<Symbol>;
                            l->push_back(*tmp);
                            cout<<"save "<<tmp->gettmp()<<endl;
                            cout<<"addr  "<<l<<endl;
                            cout<<"symbol addr  "<<tmp<<endl;

                            $$=l;

                            }
| '=' STRING             {printf("get string = %s\n", $2);


                        }

;
%%

#include <ctype.h>

void yyerror (char const *s) {
    
    //extern int yylineno;    
    //extern char *yytext;    
    //int len=strlen(yytext);
    //int i;
    //char buf[512]={0};
    //for (i=0;i<len;++i)
    //{
    //    sprintf(buf,"%s%c",buf,yytext[i]);
    //}
    //fprintf(stderr, "ERROR: %s at symbol '%s' on line %d, str is %s \n", s, buf, yylineno, yytext);
    
    //fprintf(stderr, "ERROR \"%s\" \n", yytext);
}
 

int main (void) {
    lexmain();
    yyparse();
}